using System;
using System.Numerics;
using System.Linq;

namespace ABA
{
    public static class Base36Converter
    {
        public static string BinaryToBase36(string binaryString)
        {
            // 验证输入是否为有效的二进制字符串
            if (string.IsNullOrEmpty(binaryString))
                throw new ArgumentException("输入字符串不能为空");
            if (binaryString.Any(c => c != '0' && c != '1'))
                throw new ArgumentException("输入包含非二进制字符");

            BigInteger decimalValue = BinaryStringToBigInteger(binaryString);
            return ToBase36(decimalValue);
        }

        private static BigInteger BinaryStringToBigInteger(string binary)
        {
            BigInteger result = 0;
            foreach (char c in binary)
            {
                result <<= 1; // 左移一位，相当于乘以2
                if (c == '1')
                    result += 1;
            }
            return result;
        }

        private static string ToBase36(BigInteger value)
        {
            const string base36Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            if (value == 0)
                return "0";

            string result = "";
            while (value > 0)
            {
                int remainder = (int)(value % 36);
                result = base36Chars[remainder] + result; // 将余数对应的字符添加到结果前面
                value /= 36;
            }
            return result;
        }
    }
}